package com.eclink.asset.hardware.controller;

import com.eclink.asset.domain.query.WarehouseQuery;
import com.eclink.asset.hardware.domain.dao.Hardware;
import com.eclink.asset.hardware.domain.dto.DeviceDetailDTO;
import com.eclink.asset.hardware.service.RfidService;
import com.eclink.asset.hardware.service.HardwareService;
import com.ruoyi.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

/**
 * 硬件Controller
 * 
 * @author chenshixian
 * @date 2024-08-23
 */
@Api(tags="硬件管理接口")
@RestController
@RequestMapping("/hardware")
public class HardwareController extends BaseController {

    @Resource
    private HardwareService service;

    @Resource
    private RfidService rfidService;

    @ApiOperation("查询")
    @PostMapping("/list")
    public ResponseEntity<List<Hardware>> list(@RequestBody WarehouseQuery query) {
        List<Hardware> list = service.selectList(query);
        for(Hardware hardware : list) {
            Integer status = rfidService.getStatus(hardware.getNumber());
            hardware.setStatus(status);
        }
        return ResponseEntity.ok(list);
    }

    @ApiOperation("查询所有")
    @GetMapping("/listAll")
    public ResponseEntity<List<Hardware>> listAll() {
        List<Hardware> list = service.selectListAll();
        return ResponseEntity.ok(list);
    }

    @ApiOperation("获取详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<Hardware> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增")
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody Hardware entity) {

        return ResponseEntity.ok(service.insert(entity));
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody Hardware warehouse) {
        return ResponseEntity.ok(service.update(warehouse));
    }

    @ApiOperation("删除")
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        service.deleteByIds(ids);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("启动")
    @PutMapping("/start/{id}")
    public ResponseEntity<Integer> start(@PathVariable Long id) {
        service.start(id);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("启动")
    @PutMapping("/shutdown/{id}")
    public ResponseEntity<Integer> shutdown(@PathVariable Long id) {
        service.shutdown(id);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("获取消息列表")
    @GetMapping(value = "/getDeviceDetail/{id}")
    public ResponseEntity<DeviceDetailDTO> getDeviceDetail(@PathVariable Long id) {
        DeviceDetailDTO dto = rfidService.getDetail(id);
        return ResponseEntity.ok(dto);
    }

}
